package com.example.project.projectspringcloud.controller.admin.user;

import com.example.project.projectspringcloud.common.Constants;
import com.example.project.projectspringcloud.common.OperationException;
import com.example.project.projectspringcloud.entity.Page;
import com.example.project.projectspringcloud.entity.RespBody;
import com.example.project.projectspringcloud.entity.user.Menu;
import com.example.project.projectspringcloud.entity.user.RelationRoleMenu;
import com.example.project.projectspringcloud.entity.user.Role;
import com.example.project.projectspringcloud.service.user.PermissionService;
import com.example.project.projectspringcloud.service.user.UserService;
import com.example.project.projectspringcloud.utils.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 权限管理类
 */
@Api(tags = "权限管理控制器")
@Controller
@RequestMapping(value = "/admin/permission")
public class AdminPermissionController {

    @Autowired
    private PermissionService permissionService;

    @Autowired
    private UserService userService;

    @ApiOperation(value = "角色列表", notes = "返回角色列表")
    @GetMapping(value = "/role/list")
    public String roleList(Role role, Model model) throws Exception {
        List<Role> roles = permissionService.roleList(role);
        model.addAttribute("roles", roles);
        model.addAttribute("role", role);
        return "admin/user/user-role-list";
    }

    @ApiOperation(value = "角色表单", notes = "返回角色表单")
    @GetMapping(value = "/role/form")
    public String roleForm(@RequestParam(value = "id", required = false) Integer id, Model model) throws Exception {
        Role role = permissionService.getRole(new Role(id));
        model.addAttribute("role", role);
        return "admin/user/user-role-form";
    }

    @ApiOperation(value = "API保存角色", notes = "根据ID判断更新/插入")
    @PostMapping(value = "/role/save")
    @ResponseBody
    public RespBody roleSave(Role role) throws Exception {
        try {
            boolean result = permissionService.saveRole(role);
            if (!result) {
                return RespBody.error(Constants.Message.SAVE_FAILED);
            }
        } catch (OperationException e) {
            return RespBody.error(e.getMessage());
        }

        return RespBody.ok(Constants.Message.SAVE_SUCCESS);
    }

    @ApiOperation(value = "API角色删除", notes = "删除角色id/ids")
    @DeleteMapping(value = "/role/delete/{ids}")
    @ResponseBody
    public RespBody roleDelete(@ApiParam(value = "角色ID字符串 1,2,3...") @PathVariable("ids") String ids) {
        if (StringUtils.isBlank(ids)) {
            return RespBody.error(Constants.Message.PARAM_INVALID);
        }
        if (!permissionService.deleteRole(StringUtil.split(ids, Constants.Punctuation.COMMA))) {
            return RespBody.error(Constants.Message.DELETE_FAILED);
        }
        return RespBody.ok(Constants.Message.DELETE_SUCCESS);
    }

    @ApiOperation(value = "菜单列表", notes = "返回菜单列表")
    @GetMapping(value = "/menu/list")
    public String menuList(Menu menu, Model model) throws Exception {
        List<Role> roles = permissionService.roleList(new Role());
        model.addAttribute("roles", roles);
        List<Menu> menus = permissionService.menuList(menu);
        model.addAttribute("menus", menus);
        model.addAttribute("menu", menu);
        return "admin/user/user-menu-list";
    }

    @ApiOperation(value = "菜单表单", notes = "返回菜单表单")
    @GetMapping(value = "/menu/form")
    public String menuForm(@RequestParam(value = "id", required = false) Integer id, Model model) throws Exception {
        Menu menu = permissionService.getMenu(new Menu(id));
        model.addAttribute("menu", menu);
        return "admin/user/user-menu-form";
    }

    @ApiOperation(value = "API保存菜单", notes = "根据ID判断更新/插入")
    @PostMapping(value = "/menu/save")
    @ResponseBody
    public RespBody menuSave(Menu menu) throws Exception {
        try {
            boolean result = permissionService.saveMenu(menu);
            if (!result) {
                return RespBody.error(Constants.Message.SAVE_FAILED);
            }
        } catch (OperationException e) {
            return RespBody.error(e.getMessage());
        }

        return RespBody.ok(Constants.Message.SAVE_SUCCESS);
    }

    @ApiOperation(value = "API菜单删除", notes = "根据ID删除")
    @DeleteMapping(value = "/menu/delete/{ids}")
    @ResponseBody
    public RespBody menuDelete(@ApiParam(value = "id/ids 1,2,3...") @PathVariable("ids") String ids) {
        if (StringUtils.isBlank(ids)) {
            return RespBody.error(Constants.Message.PARAM_INVALID);
        }
        Integer[] idarr = StringUtil.split(ids, ",");
        if (!permissionService.deleteMenu(idarr)) {
            return RespBody.error(Constants.Message.DELETE_FAILED);
        }
        return RespBody.ok(Constants.Message.DELETE_SUCCESS);
    }

    @ApiOperation(value = "API获取角色菜单关系", notes = "根据角色ID获取菜单")
    @GetMapping(value = "/relationrolemenu/{id}")
    @ResponseBody
    public RespBody getRelationRoleMenu(@ApiParam(value = "角色ID") @PathVariable("id") Integer id) throws Exception {
        try {
            List<Integer> menuIds = permissionService.getMenuListByRoleId(id);
            return RespBody.ok(menuIds);
        } catch (OperationException e) {
            return RespBody.error(e.getMessage());
        }
    }

    @ApiOperation(value = "API更新角色菜单关联", notes = "更新角色对应菜单关联")
    @RequestMapping(value = "/relationrolemenu/update/{roleId}")
    @ResponseBody
    public RespBody updateRelationRoleMenu(@PathVariable("roleId") Integer roleId, @RequestParam("menus[]") Integer[] menus) throws Exception {
        if(roleId == null) {
            return RespBody.error(Constants.Message.PARAM_INVALID);
        }
        try {
            permissionService.updateRelationRoleMenu(roleId, menus);
            permissionService.selectAllMenuByRole(true);
            return RespBody.ok(Constants.Message.SAVE_SUCCESS);
        } catch (OperationException e) {
            return RespBody.error(e.getMessage());
        }
    }

}
